package com.geese.module.business.service.impl;

import com.geese.common.constants.BusinessConstants;
import com.geese.common.constants.Constants;
import com.geese.common.entity.Business;
import com.geese.common.entity.ChannelDO;
import com.geese.common.util.BeanUtil;
import com.geese.common.util.MD5Util;
import com.geese.common.vo.Page;
import com.geese.exception.BusinessException;
import com.geese.module.business.dao.BusinessMapper;
import com.geese.module.business.dao.ChannelMapper;
import com.geese.module.business.dto.BusinessDTO;
import com.geese.module.business.service.IBusinessService;
import com.geese.module.business.vo.BusinessVO;
import com.geese.module.user.dao.AccountMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import ma.glasnost.orika.metadata.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by huweiwei on 2017/3/17.
 */
@Service
public class BusinessServiceImpl implements IBusinessService {

    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private ChannelMapper channelMapper;
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Map<String, Object> selectBusinessList(Page page, BusinessVO businessVO) {
        PageHelper.startPage(page.getCurrPage(), page.getPageSize(), " id DESC");
        List<Business> businessList = businessMapper.findAll(businessVO);
        Type<Business> sourceType = BeanUtil.getType(Business.class);
        Type<BusinessDTO> targetType = BeanUtil.getType(BusinessDTO.class);
        List<BusinessDTO> data = BeanUtil.mapList(businessList, sourceType, targetType);

        PageInfo pages = new PageInfo(businessList);
        Map<String, Object> pageInfo = Maps.newHashMap();
        pageInfo.put(Constants.CURR_PAGE, pages.getPageNum());
        pageInfo.put(Constants.TOTAL_PAGE, pages.getPages());
        pageInfo.put(Constants.TOTAL, pages.getTotal());

        Map<String, Object> result = Maps.newHashMap();
        result.put(Constants.PAGE_INFO, pageInfo);
        result.put("businessList", data);
        return result;
    }

    @Override
    public void updateLock(int id) {
        int skip = businessMapper.lockById(id);
        if (skip != 1) {
            throw new BusinessException(120034);
        }
    }

    @Override
    public void updateLockAll(List<Integer> ids) {
        int skip = businessMapper.lockByIds(ids);
        if (skip != ids.size()) {
            throw new BusinessException(120034);
        }
    }

    @Override
    public void updateAudit(Business business) {
        Business check = businessMapper.selectByPrimaryKey(business.getId());
        if (check != null && check.getAudit() == 1) {
            throw new BusinessException(120112);
        }

        int skip = businessMapper.updateByPrimaryKeySelective(business);
        if (skip != 1) {
            throw new BusinessException(120034);
        }

        //增加对应渠道人员接入商家数
        ChannelDO channelDO = channelMapper.selectByUserId(business.getChannelId());
        if (channelDO == null) {
            throw new BusinessException(120110);
        }
        channelDO.setQuantity(channelDO.getQuantity() + 1);
        channelMapper.update(channelDO);
    }

    @Override
    public void updateAuditAll(List<Integer> ids) {
        int skip = businessMapper.auditByIds(ids);
        if (skip != ids.size()) {
            throw new BusinessException(120034);
        }
    }

    @Override
    public void updateBusiness(Business business) {
        Business check = businessMapper.selectByPrimaryKey(business.getId());
        if (check.getAudit() == 1) {
            throw new BusinessException(120035);
        }
        int skip = businessMapper.updateByPrimaryKeySelective(business);
        if (skip != 1) {
            throw new BusinessException(120036);
        }
    }

    @Override
    public void addBusiness(Business business) {
        business.setBlock(0);
        business.setEnable(1);
        business.setAudit(0);
        businessMapper.insert(business);
        if (business.getId() == null || business.getId() <= 0) {
            throw new BusinessException(120033);
        }
    }

    @Override
    public Map<String, Object> selectAll() {
        List<Business> businessList = businessMapper.findAll(new BusinessVO());
        List<Map<String, Object>> data = Lists.newArrayList();
        for (Business business : businessList) {
            Map<String, Object> temp = Maps.newHashMap();
            temp.put("id", business.getId());
            temp.put("code", business.getCode());
            temp.put("name", business.getName());
            temp.put("mobile", business.getMobile());
            data.add(temp);
        }
        Map<String, Object> list = Maps.newHashMap();
        list.put("list", data);
        return list;
    }

    @Override
    public void doReset(Integer id) {
        Business business = businessMapper.selectByPrimaryKey(id);
        if (business == null){
            throw new BusinessException(120069);
        }
        accountMapper.updatePwd(business.getUserId(), MD5Util.encrypt(BusinessConstants.DEFAULT_PWD));
    }
}
